graphicsSettings = {
    { "crt", false, "crt" },
    { "rays", false, "godsray" },
    { "vignette", false, "vignette"},
    { "posterize", false, "posterize" },

    selected = 1,
    noSettings = 4,
}

local mj, mi = love.getVersion()
if mi >= 10 then
  graphicsSettings[5] = { "oldschool", false, "dmg" }
  graphicsSettings[6] = { "bloom", false, "glow" }

  graphicsSettings.noSettings = 6
end

function graphicsSettings.update(key)
    if key == ACTION.UP then
        graphicsSettings.selected = graphicsSettings.selected - 1

        if graphicsSettings.selected == 0 then
            graphicsSettings.selected = graphicsSettings.noSettings + 1
        end
    elseif key == ACTION.DOWN then
        graphicsSettings.selected = graphicsSettings.selected + 1

        if graphicsSettings.selected == graphicsSettings.noSettings + 2 then
            graphicsSettings.selected = 1
        end
    elseif key == ACTION.LEFT or key == ACTION.RIGHT then
        local opt = graphicsSettings[graphicsSettings.selected]

        if opt then
            -- Toggle the boolean
            opt[2] = not opt[2]

            -- Toggle the shader
            if opt[2] then
                fx.enable(opt[3])
            else
                fx.disable(opt[3])
            end
        else
            -- "return to game"
            state = "play"
            graphicsSettings.selected = 1
        end

        sounds.toggle:play()
    end
end

function graphicsSettings.render(text)
    local s = "GRAPHICS SETTINGS\n\n\n"

    for i, v in ipairs(graphicsSettings) do
        s = s .. leftpad(v[1], 10) .. "  "

        if graphicsSettings.selected == i then
            s = s .. "["
        end

        if v[2] then s = s .. "ON"
        else s = s .. "off" end

        if graphicsSettings.selected == i then
            s = s .. "]"
        end

        s = s .. "\n"
    end

    s = s .. "\n\n "

    if graphicsSettings.selected == graphicsSettings.noSettings + 1 then
        s = s .. "["
    end

    s = s .. "return to game"

    if graphicsSettings.selected == graphicsSettings.noSettings + 1 then
        s = s .. "]"
    end

    drawTitle(s, text)
end

function leftpad(str, len, chr)
    local chr = chr or " "
    local l = len - #str
    if l > 0 then return string.rep(chr, l) .. str
    else return str end
end
